package tracing

import (
	"github.com/opentracing/opentracing-go"
	"github.com/uber/jaeger-client-go"
	"github.com/uber/jaeger-client-go/config"
	jaegercfg "github.com/uber/jaeger-client-go/config"
	jaegerlog "github.com/uber/jaeger-client-go/log"
	"io"
	config2 "operator/pkg/config"
	"operator/pkg/format"
	"os"
	"time"
)

type JaegerTracing struct {
	tracer opentracing.Tracer
	closer io.Closer
}

func (j *JaegerTracing) Tracer() opentracing.Tracer {
	return j.tracer
}

func (j *JaegerTracing) StartSpan(opname string, parentSpan opentracing.SpanContext) opentracing.Span {
	opt := make([]opentracing.StartSpanOption, 0, 10)
	opt = append(opt, opentracing.StartTime(time.Now()))
	if parentSpan != nil {
		opt = append(opt, opentracing.ChildOf(parentSpan))
	}
	span := j.tracer.StartSpan(opname, opt...)

	return span
}

func NewJaegerByIp(ip, servicename string) *JaegerTracing {
	cfg := &config.Configuration{
		ServiceName: servicename,
		Sampler: &config.SamplerConfig{
			Type:  jaeger.SamplerTypeConst,
			Param: 1,
		},
		Reporter: &config.ReporterConfig{
			//CollectorEndpoint: "http:" + conf.ServiceIp + ":14268/api/traces",
			LogSpans:           true,
			LocalAgentHostPort: ip + ":6831",
		},
	}
	format.Println("pkg/tracing/jaeger.go", 82, "jaeger collection endpoint: http:"+ip+":6831")

	jLogger := jaegerlog.StdLogger
	tracer, closer, err := cfg.NewTracer(
		jaegercfg.Logger(jLogger),
	)
	if err != nil {
		format.Errors("pkg/tracing/jaeger.go", 57, err.Error())
		os.Exit(111)
	}

	j := &JaegerTracing{
		closer: closer,
		tracer: tracer,
	}

	return j
}

func NewJaegerTracing(conf *config2.SidecarConfig) *JaegerTracing {
	cfg := &config.Configuration{
		ServiceName: conf.ServiceName,
		Sampler: &config.SamplerConfig{
			Type:  jaeger.SamplerTypeConst,
			Param: 1,
		},
		Reporter: &config.ReporterConfig{
			//CollectorEndpoint: "http:" + conf.ServiceIp + ":14268/api/traces",
			LogSpans:           true,
			LocalAgentHostPort: conf.ServiceIp + ":6831",
		},
	}
	format.Println("pkg/tracing/jaeger.go", 82, "jaeger collection endpoint: http:"+conf.ServiceIp+":14268/api/traces")

	jLogger := jaegerlog.StdLogger
	tracer, closer, err := cfg.NewTracer(
		jaegercfg.Logger(jLogger),
	)
	if err != nil {
		format.Errors("pkg/tracing/jaeger.go", 89, err.Error())
		os.Exit(111)
	}

	j := &JaegerTracing{
		closer: closer,
		tracer: tracer,
	}

	return j
}

func LocalHostTracing() *JaegerTracing {
	cfg := &config.Configuration{
		ServiceName: "test-service",
		Sampler: &config.SamplerConfig{
			Type:  jaeger.SamplerTypeConst,
			Param: 1,
		},
		Reporter: &config.ReporterConfig{
			//CollectorEndpoint: "http://192.168.189.128:32666/api/traces",
			LogSpans:           true,
			LocalAgentHostPort: "192.168.189.128:32300",
		},
	}

	jLogger := jaegerlog.StdLogger
	tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jLogger))
	if err != nil {
		format.Errors("pkg/tracing/jaeger.go", 118, err.Error())
		os.Exit(111)
	}

	j := &JaegerTracing{
		closer: closer,
		tracer: tracer,
	}

	return j
}
